﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.Security
{
    public class Signup
    {
        public static DataTransfer.Security.Signup.PageLoad PageLoad()
        {
            var ret = new DataTransfer.Security.Signup.PageLoad(); // Create Output Class
            return ret;
        }

        public static DataTransfer.Security.Signup.FormOutput TrySignup(DataTransfer.Security.Signup.FormInput input)
        {
            var ret = new DataTransfer.Security.Signup.FormOutput(); // Create Output Class

            # region Validate Input
            bool isInputValid = true;

            //// Validate FirstName
            //if (Shared.Validation.Empty(input.FirstName, 1, ref ret.FirstNameValidationCode, ref isInputValid))
            //{
            //    if (Shared.Validation.Size(input.FirstName, 50, 2, ref ret.FirstNameValidationCode, ref isInputValid))
            //    {
            //    }
            //}

            //// Validate Email
            //if (Shared.Validation.Empty(input.LastName, 1, ref ret.LastNameValidationCode, ref isInputValid))
            //{
            //    if (Shared.Validation.Size(input.LastName, 50, 2, ref ret.LastNameValidationCode, ref isInputValid))
            //    {
            //    }
            //}

            //// Validate Email
            //if (Shared.Validation.Empty(input.Email, 1, ref ret.EmailValidationCode, ref isInputValid))
            //{
            //    if (Shared.Validation.Size(input.Email, 50, 2, ref ret.EmailValidationCode, ref isInputValid))
            //    {
            //        if (Shared.Validation.RegEx(input.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", 3, ref ret.EmailValidationCode, ref isInputValid))
            //        {

            //        }
            //    }
            //}

            //// Validate Password 1
            //if (Shared.Validation.Empty(input.Password1, 1, ref ret.Password1ValidationCode, ref isInputValid))
            //{
            //    if (Shared.Validation.Size(input.Password1, 50, 2, ref ret.Password1ValidationCode, ref isInputValid))
            //    {
 
            //    }
            //}

            //// Validate Password 2
            //if (Shared.Validation.Empty(input.Password2, 1, ref ret.Password2ValidationCode, ref isInputValid))
            //{
            //    if (Shared.Validation.Size(input.Password2, 50, 2, ref ret.Password2ValidationCode, ref isInputValid))
            //    {
            //        if (Shared.Validation.IsEqual(input.Password1, input.Password2, 3, ref ret.Password2ValidationCode, ref isInputValid))
            //        {

            //        }
            //    }
            //}

            #endregion

            if (isInputValid)
            {
                var data = DataAccess.Security.Signup.GetUserInfo(input.Email); // See if user exists

                if (data.NotExists)
                {
                    string passwordSalt = Shared.Encryption.GenerateSalt(16);
                    string passwordHash = Shared.Encryption.HashPassword(input.Password1, passwordSalt);

                    var save = new DataTransfer.Security.Signup.SaveUserInfo() { 
                        FirstName = input.FirstName,
                        LastName = input.LastName,
                        Email = input.Email,
                        PasswordHash = passwordHash,
                        PasswordSalt = passwordSalt
                    };
                    DataAccess.Security.Signup.SaveUser(save);
                    Email.QueueEmail("Confirm Registration", "admin@adamscenter.org", input.Email, "Click here to confirm your email", 1);
                    ret.IsValid = true;
                }
                else
                {
                    ret.EmailTaken = true;
                }
            }
            else
            {
                // User input invalid
                ret.IsInvalidInput = true;
            }

            return ret;
        }

        public static DataTransfer.Security.Signup.CompletePageLoad PageLoad(DataTransfer.Security.Signup.CompletePageLoad input)
        {
            var ret = new DataTransfer.Security.Signup.CompletePageLoad(); // Create Output Class
            ret.Email = input.Email;
            return ret;
        }
    }
}
